package cc.ok200.muzhi;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class PluginAdapter extends RecyclerView.Adapter<PluginAdapter.PluginViewHolder> {

    private static final String TAG = "PluginAdapter";
    private Context context;
    private JSONArray plugins;
    private OnItemClickListener onItemClickListener;

    public PluginAdapter(Context context, JSONArray plugins) {
        this.context = context;
        this.plugins = plugins;
    }

    @NonNull
    @Override
    public PluginViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_plugin, parent, false);
        return new PluginViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PluginViewHolder holder, int position) {
        try {
            JSONObject plugin = plugins.getJSONObject(position);
            
            holder.tvPluginType.setText(plugin.getString("type"));
            holder.tvPluginName.setText(plugin.getString("name"));
            holder.tvPluginDescription.setText(plugin.getString("description"));
            
            // 显示昵称
            if (plugin.has("nickname")) {
                holder.tvPluginNickname.setText(plugin.getString("nickname"));
                holder.tvPluginNickname.setVisibility(View.VISIBLE);
            } else {
                holder.tvPluginNickname.setVisibility(View.GONE);
            }
            
            // 使用GlideApp加载图片
            try {
                RequestOptions options = new RequestOptions()
                    .placeholder(R.mipmap.ic_launcher)
                    .error(R.mipmap.ic_launcher)
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
                    
                GlideApp.with(context)
                    .load(plugin.getString("avatar"))
                    .apply(options)
                    .into(holder.ivPluginIcon);
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(context, "图片加载失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            }
            
            // 设置点击事件
            holder.itemView.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(position, plugin);
                }
            });
        } catch (JSONException e) {
            Log.e(TAG, "JSON解析错误: " + e.getMessage());
        }
    }

    @Override
    public int getItemCount() {
        return plugins != null ? plugins.length() : 0;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, JSONObject plugin);
    }

    static class PluginViewHolder extends RecyclerView.ViewHolder {
        ImageView ivPluginIcon;
        TextView tvPluginType;
        TextView tvPluginName;
        TextView tvPluginDescription;
        TextView tvPluginNickname;

        public PluginViewHolder(@NonNull View itemView) {
            super(itemView);
            ivPluginIcon = itemView.findViewById(R.id.iv_plugin_icon);
            tvPluginType = itemView.findViewById(R.id.tv_plugin_type);
            tvPluginName = itemView.findViewById(R.id.tv_plugin_name);
            tvPluginDescription = itemView.findViewById(R.id.tv_plugin_description);
            tvPluginNickname = itemView.findViewById(R.id.tv_plugin_nickname);
        }
    }
} 